<?php
    /**
     *  
     */
    class BlogArticle extends BaseModel
    {

        CONST STATUS_ALL        = -1;
        CONST STATUS_OPEN       = 1;
        CONST STATUS_CLOSE      = 2;
        CONST STATUS_DELETE     = 9;

        protected $_id;
        protected $_userId;
        protected $_blogId;
        protected $_status;
        protected $_createDate;
        protected $_updateDate;
        protected $_properties;

        /**
         *  讀取資料表而產生, 請視同cache, 任何情況下皆不可更動該產生的內容
         *  @return Array()
         */
        public static function getTableDefinition()
        {
        }

        /**
         *  經資料表傳至程式中時所對應的 method 資訊
         *  @return Array()
         */
        public static function getTableFields()
        {
            return Array(
                "id"                    => "getId",
                "user_id"               => "getUserId",
                "blog_id"               => "getBlogId",
                "status"                => "getStatus",
                "create_date"           => "getCreateDateByDb",
                "update_date"           => "getUpdateDateByDb",
                "properties"            => "getProperties"
            );
        }

        /**
         *  reset to default value model
         *  @return Array()
         */
        public function resetValue() {
            $this->__sleep();
            $this->setId                   ( -1 );
            $this->setUserId               ( NULL );
            $this->setBlogId               ( NULL );
            $this->setStatus               ( BlogArticle::STATUS_OPEN );
            $this->setCreateDate           ( time() );
            $this->setUpdateDate           ( time() );
            $this->setProperties           ( Array() );
        }

        /**
         *  validate model data
         *  @return messages Array()
         */
        public function validate()
        {
            $validateInfo = parent::validate();

            if( null===$this->getUserId() ) {
                $validateInfo['userId']['message'] = '不能空值';
            }
            if( null===$this->getBlogId() ) {
                $validateInfo['blogId']['message'] = '不能空值';
            }

            // timestamp
            if( $this->getCreateDate() < -28800 ) {
                $validateInfo['createDate']['message'] = '日期不正確';
            }
            
            // timestamp
            if( $this->getUpdateDate() < -28800 ) {
                $validateInfo['updateDate']['message'] = '日期不正確';
            }
            
            return $validateInfo;
        }

        /**
         *  filter model data
         */
        public function filter()
        {
            $this->setUserId( (int) $this->getUserId() );
            $this->setBlogId( (int) $this->getBlogId() );
            $this->setStatus( (int) $this->getStatus() );
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            basic method 
        ------------------------------------------------------------------------------------------------------------------------ */

        /**
         *  get UserId 
         *  @return int
         */
        public function getUserId()
        {
            return $this->_userId;
        }
        /**
         *  set UserId
         */
        public function setUserId( $userId )
        {
            $this->_userId = (int) $userId;
        }

        /**
         *  get BlogId 
         *  @return int
         */
        public function getBlogId()
        {
            return $this->_blogId;
        }
        /**
         *  set BlogId
         */
        public function setBlogId( $blogId )
        {
            $this->_blogId = (int) $blogId;
        }

        /**
         *  get Status 
         *  @return int
         */
        public function getStatus()
        {
            return $this->_status;
        }
        /**
         *  set Status
         */
        public function setStatus( $status )
        {
            $this->_status = (int) $status;
        }





        /* ------------------------------------------------------------------------------------------------------------------------
            extends
        ------------------------------------------------------------------------------------------------------------------------ */
        /**
         *  clear extends information
         */
        public function __sleep()
        {
            $this->_blog = null;
            $this->_articleText = null;
            return parent::__sleep();
        }

        /**
         *  get blog model
         *  @param  boolean - isSave , is cache the object
         *  @return object
         */
        public function getBlog( $isSave=true )
        {
            if( !$isSave ) {
                $this->_blog = null;
            }
            if( $this->_blog ) {
                return $this->_blog;
            }

            $blogs = new Blogs();
            $blog = $blogs->getBlog( $this->getBlogId() );

            if( $isSave ) {
                $this->_blog = $blog;
            }
            return $blog;
        }

        /**
         *  get blog model
         *  @param  boolean - isSave , is cache the object
         *  @return object
         */
        public function getArticleText( $isSave=true )
        {
            if( !$isSave ) {
                $this->_articleText = null;
            }
            if( $this->_articleText ) {
                return $this->_articleText;
            }

            $blogArticleTexts = new BlogArticleTexts();
            $blogArticleText = $blogArticleTexts->getBlogArticleTextByArticleId( $this->getId() );

            if( $isSave ) {
                $this->_articleText = $blogArticleText;
            }
            return $blogArticleText;
        }


    }

